ethers-middleware
Your ethers application interacts with the blockchain through a Provider
abstraction. Provider
is a special type of Middleware
that can be composed with others to obtain a layered architecture. This approach promotes "Open Closed Principle", "Single Responsibility" and composable patterns. The building process happens in a wrapping fashion, and starts from a Provider
being the first element in the stack. This process continues having new middlewares being pushed on top of a layered data structure.
For more information, please refer to the book.
Warning
This library is in the process of being deprecated. See #2667 for more information.
Available Middleware
Signer
: Signs transactions locally, with a private key or a hardware wallet.Nonce Manager
: Manages nonces locally. Allows to sign multiple consecutive transactions without waiting for them to hit the mempool.Gas Escalator
: Bumps transactions gas price in the background to avoid getting them stuck in the memory pool. AGasEscalatorMiddleware
supports different escalation strategies (see GasEscalator) and bump frequencies (see Frequency).Gas Oracle
: Allows getting your gas price estimates from places other thaneth_gasPrice
, including REST based gas stations (i.e. Etherscan, ETH Gas Station etc.).Transformer
: Allows intercepting and transforming a transaction to be broadcasted via a proxy wallet, e.g.DSProxy
.
Examples
Each Middleware
implements the trait MiddlewareBuilder. This trait helps a developer to compose a custom Middleware
stack.
The following example shows how to build a composed Middleware
starting from a Provider
:
# use ;
# use ;
# use ;
let key = "fdb33e2105f08abe41a8ee3b758726a31abdd57b7a443f470f23efce853af169";
let signer = key.?;
let address = signer.address;
let gas_oracle = new;
let provider = try_from?
.gas_oracle
.with_signer
.nonce_manager; // Outermost layer
# Ok::
The wrap_into function can be used to wrap Middleware
layers explicitly. This is useful when pushing Middleware
s not directly handled by the builder interface.
# use ;
# use TryFrom;
# use ;
# use ;
let key = "fdb33e2105f08abe41a8ee3b758726a31abdd57b7a443f470f23efce853af169";
let signer = key.?;
let address = signer.address;
let escalator = new;
let provider = try_from?
.wrap_into
.wrap_into
.wrap_into
.wrap_into; // Outermost layer
# Ok::
A Middleware
stack can be also constructed manually. This is achieved by explicitly wrapping layers.
# use ;
# use ;
# use ;
// Start the stack
let provider = try_from?;
// Escalate gas prices
let escalator = new;
let provider = new;
// Sign transactions with a private key
let key = "fdb33e2105f08abe41a8ee3b758726a31abdd57b7a443f470f23efce853af169";
let signer = key.?;
let address = signer.address;
let provider = new;
// Use GasNow as the gas oracle
let gas_oracle = new;
let provider = new;
// Manage nonces locally
let provider = new;
# Ok::